package co.baiku.boot.wechat.properties;

import co.baiku.boot.wechat.enums.HttpClientType;
import co.baiku.boot.wechat.enums.StorageType;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.io.Serializable;

/**
 * 微信接入相关配置属性.
 *
 * @author someone
 */
@Data
@ConfigurationProperties(WxMpProperties.PREFIX)
public class WxMpProperties {
    public static final String PREFIX = "wx.mp";

    /**
     * 设置微信公众号的appid.
     */
    private String appid;

    /**
     * 设置微信公众号的app secret.
     */
    private String secret;

    /**
     * 设置微信公众号的token.
     */
    private String token;

    /**
     * 设置微信公众号的EncodingAESKey.
     */
    private String aesKey;

    /**
     * 自定义host配置
     */
    private HostConfig hosts;

    /**
     * sns回调地址
     */
    private String snsRedirectUri;


    private String sns_code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";

    /**
     * 获取公众号授权地址
     */
    public String oauthUrl(String state) {
        return String.format(sns_code_url, snsRedirectUri, "snsapi_userinfo", state);
    }


    /**
     * 获取公众号静默授权
     */
    public String oauthBaseUrl(String state) {
        return String.format(sns_code_url, snsRedirectUri, "snsapi_base", state);
    }

    /**
     * 存储策略
     */
    private final ConfigStorage configStorage = new ConfigStorage();

    @Data
    public static class ConfigStorage implements Serializable {
        private static final long serialVersionUID = 4815731027000065434L;

        /**
         * 存储类型.
         */
        private StorageType type = StorageType.Memory;

        /**
         * 指定key前缀.
         */
        private String keyPrefix = "wx";

        /**
         * redis连接配置.
         */
        @NestedConfigurationProperty
        private final RedisProperties redis = new RedisProperties();

        /**
         * http客户端类型.
         */
        private HttpClientType httpClientType = HttpClientType.HttpClient;

        /**
         * http代理主机.
         */
        private String httpProxyHost;

        /**
         * http代理端口.
         */
        private Integer httpProxyPort;

        /**
         * http代理用户名.
         */
        private String httpProxyUsername;

        /**
         * http代理密码.
         */
        private String httpProxyPassword;

    }

}
